<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
	
	
        public function __construct(){
      	parent::__construct();
      	$this->load->database();
      	$this->load->model('welcome_model');
      	$this->load->library('pagination');
      	$this->load->helper('form');
      }
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index(){	
		//$this->load->view('newfile');
		if($this->session->userdata('username1')){
			
		$this->load->view('welcome_view');
		}else{	
			echo "请先登录";	
		}
		
	}

	public function add1(){
	   $nd['content'] = $_POST['content'];
       $nd['email'] = $_POST['email'];  
        $nd['label'] = $_POST['label']; 
        $nd['session_id'] = $this->session->userdata('id');
       $nd['add_time']=date('Y-m-d H:i:s');
       $nd['updata_time']=date('Y-m-d H:i:s');
       $nd['delete_time']=date('Y-m-d H:i:s');
       $insert_id=$this->welcome_model->insert($nd);
      // echo $insert_id;exit;
		define ( 'ROOT_PATH', str_replace ( 'application/controllers/welcome.php', '', str_replace ( '\\', '/', __FILE__ ) ) );
		$dir = ROOT_PATH . 'uploads';			   
		$config ['upload_path'] = $dir;
		//echo $config ['upload_path'];exit;
		$config ['allowed_types'] = 'gif|jpg|png|bmp';
		$config ['max_size'] = '10000000';
		$config ['max_width'] = '0';
		$config ['max_height'] = '0';
		$config['encrypt_name']  = true;
		 $this->load->library('upload',$config);
		$this->upload->initialize ($config);
		//
		
	foreach ( $_FILES as $k => $v ) {
			echo ($k);exit;
	//print_r($this->upload->do_upload ( $k ));exit;
		
		if ($k ['error'] == 0) {
		if (! $this->upload->do_upload ( $k )) {
			$data1 = '';
		} else {
	    	$data1 = $this->upload->data ();
									
		///////////////////////////////////////////////////////////							//////////////////////////////////
		$image_width=$data1['image_width'];
		$image_height=$data1['image_height'];
		if ($image_width >= 300 || $image_height >= 250) {
			if ($image_width / $image_height > 300 / 250) {
				$thumb_a = $this->do_image ( '_b', $dir. '/' . $data1 ['file_name'], 300, 250 * ($image_height / $image_width) );
		} elseif ($image_width / $image_height <= 300 / 250) {
				$thumb_a = $this->do_image ( '_b', $dir. '/' . $data1 ['file_name'], 300 * ($image_width / $image_height), 250 );
	}
		} else {
				$thumb_a = $this->do_image ( '_b',$dir. '/' . $data1 ['file_name'], $image_width, $image_height );
		}
		//echo $nd['picture'];exit;
	   //$data= $this->welcome_model->insert($nd);      
	   $arr['url'] = $data1['file_name'];
    // echo "<pre>"; print_r($arr['url']);exit;
       $arr['content_id'] = $insert_id;
	  //echo $arr['content_id'];exit;
	   $this->welcome_model->image_insert($arr);
		}
		
	}
							//echo $config['source_image'];exit;
}
		
		//echo "<pre>";print_r($data1);exit;
		//echo "<pre>";
		// echo "<pre>";print_r($_FILES);exit;		
		//print_r($data1);exit;
	// print_r($data);exit;
		echo '<script>window.location.href="content" </script>';
	
 	}
							

	public function content($offset=''){	
		$pagesize = 5;			    
		$config['base_url'] = 'http://localhost/633games/index.php/welcome/content';		
        $config['total_rows'] = $this->welcome_model->total();
        //echo $config['total_rows'];
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 2;
        $config['per_page'] = $pagesize;        
        $this->pagination->initialize($config);
        $data['detial']=$this->welcome_model->detial($offset,$pagesize);
      	
       foreach($data['detial'] as $key=>$val){
       	//echo "<pre>";print_r($val);exit;
       	$data['get_images']=$this->welcome_model->get_images($val['id']);
       //	echo "<pre>";print_r($data['get_images']);exit;
       	$data['detial'][$key]['url'] =	get_thumb_pic1($data['get_images']['url'],b);
       	$data['detial'][$key]['bigurl']=get_thumb_pic1($data['get_images']['url']);	
        $username =$this->welcome_model->get_username($val['session_id']);
       $data['detial'][$key]['username']=$username['username'];
       	//echo "<pre>";print_r($data['detial']);exit;
       }
        //echo "<pre>";print_r($data['get_images']);exit;
		$data['pagination'] = $this->pagination->create_links();
		$this->load->view('detial_view',$data);
		
			}
 
			
  function list1($id=''){
  	
  	    $data['get_detial']=$this->welcome_model->get_detial($id);
        $data['list_images']=$this->welcome_model->list_images($id);
  // echo "<pre>";  print_r($data['list_images']);exit;	
        foreach ($data['list_images'] as $key=>$val){
        	// echo "<pre>";  print_r($val);exit;	
        $data['images'][$key] = get_thumb_pic1($val['url'],b);
       //$data['list_images']=get_thumb_pic1($data['get_images']['url'],b);
       // echo "<pre>";	print_r($data['images']);exit;
        }
        //echo "<pre>";	print_r($data['images']);exit;
     	$this->load->view('list_view',$data);
     }	
			
function do_image($thumb_marker,$image_name,$width=0,$height=0){
		$this->load->library('image_lib');
		$config['image_library'] = 'GD2';
		$config['source_image'] = "$image_name";
		$config['width'] = $width;
		$config['height'] = $height;
		$config['create_thumb'] = TRUE;
		$config['thumb_marker'] = "$thumb_marker";
		$this->image_lib->initialize($config);
		$this->image_lib->resize();
	}
     
    function delete1(){
    $text_id = $_POST['text_id'];
   // echo $text_id;
    $del=$this->welcome_model->del($text_id);
   // echo $del;
    if($del){
    	echo '1';
    }else{
    	echo '0'; 	
    }
    } 
    
    
    function edit($id){
    	$arr['content_edit'] = $this->welcome_model->get_detial($id);
    	
    	$this->load->view('edit_view',$arr);
    }
    
    function user_center($session_id=''){
        $array['username'] = $this->welcome_model->get_username($session_id);
      // print_r($array['username']);
    	$array['session_content'] = $this->welcome_model->get_session_content($session_id);
    	$array['name'] = $this->session->userdata('username1');
    	//echo "<pre>";	print_r($array['session_content']);exit;
    	foreach ($array['session_content'] as $key=>$item){
    		$content_id[$key]=$item['id'];
    		
    	}
    	//echo "<pre>";print_r($content_id);exit;
    	foreach ($content_id as $k=>$v){
    		$img = $this->welcome_model->list_images($v);
    		
    		foreach ($img as $kk=>$item){
    			$images[$kk] =  get_thumb_pic1($item['url'],b);
    		}
    		$array['session_content'][$k]['url'] = $images;
    		
    	}
    	$this->load->view('user_center',$array);
    }
    
    function update($id){
    	$data = array(
    	'content' => $_POST['edit_name'],
    	'email' => $_POST['edit_email'],
    	'label' => $_POST['edit_label']
    	);
    	$this->welcome_model->edit_content($id,$data);
    	//////////////////////////////////////////////////////////////////////////////////////////////////////////
    	define ( 'ROOT_PATH', str_replace ( 'application/controllers/welcome.php', '', str_replace ( '\\', '/', __FILE__ ) ) );
		$dir = ROOT_PATH . 'uploads';	
		$config['upload_path'] = $dir;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['encrypt_name']  = true;
        $this->load->library('upload', $config);
        $this->upload->initialize ($config);
       
      foreach ( $_FILES as $key1 => $value ) {
      	echo ($key1);
       print_r($this->upload->do_upload ( $key1 ));exit;
      	if ($key ['error'] == 0) {
      		if (! $this->upload->do_upload ( $key )) {
			$filename1111 = '';
			
		} else {
			echo "11111111111";
	    	$filename1111 = $this->upload->data();
	    	
               } 
          // echo "<pre>";print_r( $this->upload->data());exit;
            
     }

    }
 }

}



























/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */